<?php
namespace app\mini\controller\commons;

use app\common\model\ShopGoods;
use app\common\traits\InterfacesExceptionTrait;

class Goods extends Base
{
    use InterfacesExceptionTrait;

    /**
     * 获取所有商品
     * @return array
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $shop_id = input('shop_id',0);
        if (empty($shop_id)){
            $data = \app\common\model\Goods::field('id goods_id,goods_name')->where('goods_status',1)->select()->toArray();
            array_unshift($data,['goods_id'=>0,'goods_name'=>'全部']);
        }else{

            $goods = ShopGoods::where('shop_id',$shop_id)->column('goods_id');
            if (!empty($goods)){
                $data = \app\common\model\Goods::field('id goods_id,goods_name')->where('id','in',$goods)->select()->toArray();
                array_unshift($data,['goods_id'=>0,'goods_name'=>'全部']);
            }else{
                $data[] = ['goods_id'=>0,'goods_name'=>'全部'];
            }

        }

        return $this->response($data);
    }


}